# encoding=utf8

import os
import wps2txt as wpst


class ecfiles2txt(object):

    """docstring for ecfiles2txt"""

    def __init__(self, baseDir, func, saveDir=''):
        self.baseDir = os.path.abspath(baseDir)
        self.func = func
        self.saveDir = saveDir

    def loadAllFiles(self):
        dirs, fileName = os.path.split(self.baseDir)

        saveDirs = ''
        if self.saveDir == '':
            saveDirs = os.path.join(dirs, 'new_txt')
        else:
            saveDirs = self.saveDir

        if not os.path.exists(saveDirs):
            os.makedirs(saveDirs)
            print('makedirs:' + saveDirs)

        ecfiles2txt.loadFiles(self, self.baseDir, saveDirs)

    def loadFiles(self, baseDir, saveDir):
        for files in os.listdir(baseDir):
            path = os.path.join(baseDir, files)
            if os.path.isfile(path):
                self.func(path, saveDir)
            elif os.path.isdir(path):
                ecfiles2txt.loadFiles(self, path)

if __name__ == '__main__':
    basedir = r'../testFiles'
    ef = ecfiles2txt(basedir,wpst.wps2txt)
    ef.loadAllFiles()
